package com.augurit.common.system.web.org;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.augurit.common.base.action.CrudActionSupport;
import com.augurit.common.base.action.ExtBaseAction;
import com.augurit.common.base.form.ImageForm;
import com.augurit.common.system.util.OrgUtils;

/**
 * 图片浏览器对应的Action类
 */
@SuppressWarnings("serial")
@Results({
	@Result(name = CrudActionSupport.RELOAD, location = "ac-menu.action", type = "redirect")
})
public class ImageViewerAction extends ExtBaseAction {

	public static final String IMG_PATH = "resources\\images\\icons\\";
	public static final String SMALL_IMG = "16_16";
	public static final String MIDDLE_IMG = "32_32";
	public static final String BIG_IMG = "48_48";
	
	public static final String APP_SMALL_IMAGES = "APP_SMALL_IMAGES";
	public static final String APP_MIDDLE_IMAGES = "APP_MIDDLE_IMAGES";
	public static final String APP_BIG_IMAGES = "APP_BIG_IMAGES";
	
	/**
	 * 系统首页的默认入口方法
	 */
	public String execute() throws Exception {
		return getSmallImages();
	}
	
	/**
	 * 获取小图标文件列表信息
	 */
	public String getSmallImages() throws Exception {
		List<ImageForm> fileList = loadImages(SMALL_IMG, APP_SMALL_IMAGES);
		this.extRenderListJson(fileList, true);
		return null;
	}
	
	/**
	 * 获取中图标文件列表信息
	 */
	public String getMiddleImages() throws Exception {
		List<ImageForm> fileList = loadImages(MIDDLE_IMG, APP_MIDDLE_IMAGES);
		this.extRenderListJson(fileList, true);
		return null;
	}
	
	/**
	 * 获取大图标文件列表信息
	 */
	public String getBigImages() throws Exception {
		List<ImageForm> fileList = loadImages(BIG_IMG, APP_BIG_IMAGES);
		this.extRenderListJson(fileList, true);
		return null;
	}
	
	/**
	 * 加载图标物理文件数据
	 * @param imgSize
	 * @param servletContextName
	 * @return
	 */
	private List<ImageForm> loadImages(String imgSize, String servletContextName){
		
		//项目在服务器上的绝对文件路径
		String realPath = this.getRequest().getRealPath("\\");               

		//图标在服务器上的绝对文件路径
		String relativePath = IMG_PATH + imgSize;
		String imgFolderPath = realPath + relativePath;
		
		//图标相对于项目的访问路径
		String appName = this.getRequest().getContextPath();
		String imgFolderInApp = appName + "/" + relativePath.replace("\\", "/") + "/";	//如：/framework/resources/images/icons/16_16
        
		//先检查缓存中是否存在
		List<ImageForm> list = null;
		
		if(list == null || list.size() == 0){		
			File imgFolder = new File(imgFolderPath);
			if(imgFolder.exists() && imgFolder.isDirectory()){
				
				//图标文件列表
				File[] files = imgFolder.listFiles();
				if(files != null && files.length > 0){
					list = new ArrayList();
					for(File file : files){
						ImageForm form = new ImageForm();
						form.setName(file.getName());
						form.setUrl(imgFolderInApp + form.getName());
						form.setSize(imgSize);
						
						//针对不同大小图标设定简写名称
						if(imgSize.equals(MIDDLE_IMG))
							form.setShortName(OrgUtils.getPrefixString(form.getName(), 3));
						else if(imgSize.equals(BIG_IMG))
							form.setShortName(OrgUtils.getPrefixString(form.getName(), 6));
						
						list.add(form);
					}
				}
				
				//缓存到SESSION变量中
				this.getApplication().setAttribute(servletContextName, list);
			}
		}
		
		return list;
	}
}
